/* Using JRE 1.7.0_07
 * 
 * File: ReportController.java
 * @author Wen-Fan Hsieh
 * Email: blueseadream826@gmail.com
 *
 */

package edu.bu.cs673.superkids123.parents.controller;

import java.util.*;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import edu.bu.cs673.superkids123.AbstractController;
import edu.bu.cs673.superkids123.AbstractFrame;
import edu.bu.cs673.superkids123.HibernateUtil;

import edu.bu.cs673.superkids123.children.model.CompletedGame;
import edu.bu.cs673.superkids123.children.model.CompletedLesson;
import edu.bu.cs673.superkids123.children.DataObjectAccess.ReportManager;
import edu.bu.cs673.superkids123.children.DataObjectAccess.StatManager;
import edu.bu.cs673.superkids123.common.DataObjectAccess.ChildManager;
import edu.bu.cs673.superkids123.common.model.AchievementRule.Activity;
import edu.bu.cs673.superkids123.common.model.Child;

public class ReportController extends AbstractController {

	private Child selectedChild;
	private boolean isCompleted;
	
	public ReportController(AbstractFrame mainFrame) {
		super(mainFrame);
	}
	
	public List<Child> getAllChildren() {
		
		// TODO: Get all children from database
		return ChildManager.getChildren();
	}
	
	public void setSelectedChild(String name) {
		selectedChild = ChildManager.getChildByName(name);
	}
	
	public Child getSelectedChild() {
		return selectedChild;
	}
	
	public List<CompletedLesson> getCompletedLessons() {
		
		if (selectedChild == null) {
			return new ArrayList<CompletedLesson>();
		}
		
		List<CompletedLesson> list = ReportManager.getAllCompletedLessons(selectedChild);
		
		return list;
	}
	
	public List<CompletedGame> getCompletedGame() {
		
		if (selectedChild == null) {
			return new ArrayList<CompletedGame>();
		}

		List<CompletedGame> list = ReportManager.getAllCompletedGames(selectedChild);
		
		return list;
	}
	
	public Date getTotalTime() {
		List<Child> children = ChildManager.getChildren();
		long totalTime = 0;
		
		for(int index = 0; index < children.size(); index++) {
			Child child = children.get(index);
			totalTime += StatManager.getPlayTime(child, Activity.All).getTime();
		}
		
		return new Date(totalTime);
	}
	
	public int getTotalLesson() {
		
		List<Child> children = ChildManager.getChildren();
		int lessonCount = 0;
		
		for(int index = 0; index < children.size(); index++) {
			Child child = children.get(index);
			lessonCount += StatManager.getPlayCount(child, Activity.AllLessons);
		}	
		return lessonCount;
	}
	
	public int getTotalGame() {
		List<Child> children = ChildManager.getChildren();
		int gameCount = 0;
		
		for(int index = 0; index < children.size(); index++) {
			Child child = children.get(index);
			gameCount += StatManager.getPlayCount(child, Activity.AllGames);
		}	
		return gameCount;
	}
	
	public String getMostChild() {
		List<Child> children = ChildManager.getChildren();
		long mostTime = 0;
		long totalTime = 0;
		String mostChild = null;
		
		for(int index = 0; index < children.size(); index++) {
			Child child = children.get(index);
			totalTime = StatManager.getPlayTime(child, Activity.All).getTime();
			if(totalTime > mostTime) {
				mostChild = child.getName();
				mostTime = totalTime;
			}
				
		}
		
		long sec = (mostTime / 1000) % 60;
		long min = ((mostTime / 1000) / 60) % 60;
		long hr = ((mostTime / 1000) / 60) / 60;
		
		String totalPlay = hr + " hours, " + min + " mins, " + sec + " seconds";
		
		return mostChild + ", Total Play Time: " + totalPlay;
	}
	
	/**
	 * TEST METHOD - Not for production use yet
	 * @param child
	 */
	public void getAllLessonReports(Child child) {
		List<CompletedLesson> lessonReports = ReportManager.getAllChildCompletedLessons(child, true);
	}
	
	public void getAllGameReports(Child child) {
		List<CompletedGame> gameReports = ReportManager.getAllChildCompletedGames(child, true); 
	}
}
